#define	_CRT_SECURE_NO_WARNINGS 1

#include <iostream>
#include <vector>
#include <map>
#include <limits.h>

using namespace std;

struct Graph
{
	vector<char> vertexs;
	map<char, int> indexMap;
	vector<vector<int>> matrix;
};

void GraphCreate(Graph& gh, int n)
{
	gh.vertexs.resize(n);
	gh.matrix.resize(n);

	char ch;
	for (int i = 0; i < n; ++i)
	{
		cin >> ch;
		gh.vertexs[i] = ch;
		gh.indexMap[ch] = i;
	}

	for (int i = 0; i < n; ++i)
	{
		gh.matrix[i].resize(n, INT_MAX);
	}
}

void AddEdge(Graph& gh, char srci, char dsti, int w)
{
	gh.matrix[srci - 'A'][dsti - 'A'] = w;
	gh.matrix[dsti - 'A'][srci - 'A'] = w;
}

int GetVertexIndex(Graph& gh, const char& ch)
{
	auto it = gh.indexMap.find(ch);
	if (it != gh.indexMap.end())
	{
		return it->second;
	}

	return 0;
}

void FloydWarshall(Graph& gh, vector<vector<int>>& vvDist, vector<vector<int>>& vvPath)
{
	int n = gh.vertexs.size();
	vvDist.resize(n);
	vvPath.resize(n);

	for (int i = 0; i < n; ++i)
	{
		vvDist[i].resize(n, INT_MAX);
		vvPath[i].resize(n, -1);
	}

	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < n; ++j)
		{
			if (gh.matrix[i][j] != INT_MAX)
			{
				vvDist[i][j] = gh.matrix[i][j];
				vvPath[i][j] = i;
			}

			if (i == j)
			{
				vvDist[i][j] = 0;
			}
		}
	}

	for (int k = 0; k < n; ++k)
	{
		for (int i = 0; i < n; ++i)
		{
			for (int j = 0; j < n; ++j)
			{
				if (vvDist[i][k] != INT_MAX && vvDist[k][j] != INT_MAX
					&& vvDist[i][k] + vvDist[k][j] < vvDist[i][j])
				{
					vvDist[i][j] = vvDist[i][k] + vvDist[k][j];

					vvPath[i][j] = vvPath[k][j];
				}
			}
		}
	}
}

void FarthShort(Graph& gh, const vector<vector<int>>& vvDist, int n)
{
	int max = 0;
	int min = INT_MAX;
	int mini = INT_MAX;
	for (int i = 0; i < n; ++i)
	{
		max = 0;
		for (int j = 0; j < n; ++j)
		{
			if (vvDist[i][j] > max)
			{
				max = vvDist[i][j];
			}
		}

		if (max < min)
		{
			min = max;
			mini = i;
		}
	}

	cout << gh.vertexs[mini] << endl;
}

int ShortPath(Graph& gh, const char& src, const vector<int>& dist)
{
	size_t srci = GetVertexIndex(gh, src);
	size_t n = gh.vertexs.size();

	int sum = 0;
	for (size_t i = 0; i < n; ++i)
	{
		if (i != srci)
		{
			sum += dist[i];
		}
	}

	return sum;
}

void SumShort(Graph& gh, const vector<vector<int>>& vvDist, int n)
{
	int min = INT_MAX;
	int mini = INT_MAX;
	for (int i = 0; i < n; ++i)
	{
		int sPath = ShortPath(gh, gh.vertexs[i], vvDist[i]);
		if (sPath < min)
		{
			min = sPath;
			mini = i;
		}
	}

	cout << gh.vertexs[mini];
}

int main()
{
	int n, eg;
	cin >> n;
	Graph gh;
	GraphCreate(gh, n);

	cin >> eg;
	for (int i = 0; i < eg; ++i)
	{
		char srci, dsti;
		int w;
		cin >> srci >> dsti >> w;
		AddEdge(gh, srci, dsti, w);
	}

	vector<vector<int>> vvDist, vvPath;
	FloydWarshall(gh, vvDist, vvPath);
	
	FarthShort(gh, vvDist, n);

	SumShort(gh, vvDist, n);

	return 0;
}